package team02.server.bean;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.text.SimpleDateFormat;
import team02.server.model.MySQLModel;

public class BillModel extends MySQLModel{
	private int unit_price;
	private String header="";
	private String body="";
	private String footer="";
	private String filename;
	private List<OrganizationBean> listOrg;
	public BillModel(Connection connection) {
		super(connection);
		// TODO Auto-generated constructor stub
	}
	public BillModel() {
		super(null);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int load() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int save() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	public int getUnit_price() throws SQLException
	{
		initConnection();
		String query="SELECT unit_price from jp_price ORDER BY from_date DESC";
		executeQuery(query);
		System.out.println("aaaaa");
		if(resultSet.next()){
			int t= resultSet.getInt("unit_price");
			freeResources();
			return t;
		}
		return 0;
	}
	public void setUnit_price(int price) throws SQLException{
		initConnection();
		Date dt=new Date();
		String query="INSERT INTO jp_price (unit_price, from_date) VALUES('"+price+"','"+dt.getTime()+"')";
		executeUpdate(query);
	}

	public void setHeader(String targetDate, String username, String name) throws ParseException {
		header+="CTS-TAS-GWK53M78,";
		Date date=new SimpleDateFormat("MMMM yyyy").parse(targetDate);
		int targetYear=date.getYear()+1900;
		int targetMonth=date.getMonth()+1;
		header+=Integer.toString(targetYear)+",";
		header+=Integer.toString(targetMonth)+",";
		String today=new SimpleDateFormat("yyyy,MM,dd,HH,mm,ss").format(new Date());
		header+=today+"\n";
		
	}
	public void setBody() {
		for (OrganizationBean org : listOrg) {
			String alias=org.getAlias();
			body+=alias+",";
			String name=org.getName();
			body+=name+",";
			int price=org.getPrice();
			body+=Integer.toString(price)+",";
			String address=org.getAddress();
			body+=address+",";
			String phone=org.getPhone();
			String str1="";
			String str2="";
			int j=0;
			for(int i=0; i<phone.length();i++)
			{
				if(j==4)
				{
					str1=phone.substring(0, i);
					str2=phone.substring(i);
					if(str2.length()>0)
					phone=str1+"-"+str2;
					j=0;
					i++;
				}
				j++;
			}
			body+=phone+"\n";
		}
	}
	public void setFooter(String targetDate) throws ParseException {
		footer+="END___END___END,";
		Date date=new SimpleDateFormat("MMMM yyyy").parse(targetDate);
		int targetYear=date.getYear()+1900;
		int targetMonth=date.getMonth()+1;
		footer+=Integer.toString(targetYear)+",";
		footer+=Integer.toString(targetMonth)+"\n";
	}
	public void setFileName(String targetDate) throws ParseException{
		Date date=new SimpleDateFormat("MMMM yyyy").parse(targetDate);
		int targetYear=date.getYear()+1900;
		int targetMonth=date.getMonth()+1;
		this.filename="TAS-"+targetYear+"-"+targetMonth+".csv";
	}
	public String writeCSV(String targetDate, String username, String name, String filepath) throws ParseException, IOException
	{
		setHeader(targetDate, username, name);
		setBody();
		setFooter(targetDate);
		setFileName(targetDate);
		
		//String csv = "C:\\temp\\"+filename;
		String csv=filepath+filename;
		File file=new File(csv);
		BufferedWriter writer=new BufferedWriter(new FileWriter(file));
		writer.write(header);
		writer.write(body);
		writer.write(footer);
		writer.close();
		return filename;
	}
	public List<OrganizationBean> listAllOrganizationMoney() throws SQLException{
		List<OrganizationBean> l= new OrganizationBean().listAllOrganizationMoney();
		this.listOrg=l;
		return l;
	}
	public List<OrganizationBean> listAllOrganizationMoney(String targetDate) throws SQLException, ParseException{
		List<OrganizationBean> l= new OrganizationBean().listAllOrganizationMoney(targetDate);
		this.listOrg=l;
		return l;
	}


}
